<?php
namespace app\api\controller\wsgoods;
class Goodshistory extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //我的历史记录
    public function lists(){
        $d = $this->post();
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","a.user_id","=",$this->userinfo["id"]];
        $w = where($map);
        $order = "order by a.id desc";
        $sql = "
            select
                b.id as goods_id,
                b.title goods_name,
                b.pic,
                b.price,
                b.point,
                b.goods_price_id,
                a.id as collect_id
            from
                goods_history a
            inner join
                goods b on b.id = a.tab_id
        $w[0] {$order} ".limit($d['page'],$d['limit']);
        $rs = $this->db->query($sql,$w[1],'',true);
        suc($rs[0],$rs[1]);
    }

    //添加历史记录
    public function add(){
        $d = $this->post();
        $rule = [
            'id'=>'require',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);
        $info = $this->db->query('select * from goods_history where tab_id=? and user_id=? and type=?',[$d['id'],$this->userinfo["id"],$d['type']]);
        if(!$info){
            $data = [
                'tab_id' => $d['id'],
                'user_id' => $this->userinfo["id"],
                'create_time' => time(),
            ];
            $rs = $this->db->insert('goods_history',$data);
            if (!$rs) {
                err('操作失败!');
            }
        }
        suc();
    }

    //删除历史记录
    public function del(){
        $d = $this->post();
        $rule = [
            'id'=>'require',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $this->db->query('delete from goods_history where tab_id=? and user_id=? and type=?',[$d['id'],$this->userinfo["id"],$d['type']]);
        suc("操作成功");
    }

}
?>
